package com.bsj.media.media.resample.basic;

import com.bsj.media.media.resample.resamplerState;

public class resamplerBasicDirectSingle extends resamplerBasic {

	@Override
	public int getType() {
		return 1;
	}

	@Override
	public int resampler_basic_func(resamplerState st, int channel_index, float[] in, final int offset_in, int in_len, float[] out,
									int out_len) {
		final int N = st.filt_len;
		int out_sample = 0;
		int last_sample = st.last_sample[channel_index];
		int samp_frac_num = st.samp_frac_num[channel_index] & 0xffffffff;
		final int out_stride = st.out_stride;
		final int int_advance = st.int_advance;
		final int frac_advance = st.frac_advance;
		final int den_rate = st.den_rate;
		float sum;

		while (!(last_sample >= in_len || out_sample >= out_len)) {

			int i = last_sample;
			int j = samp_frac_num * N;
			int max = j + N;
			sum = 0;
			for (j = 0; j < max; j++)
				sum += MULT16_16(st.sinc_table[j], in[offset_in+j]);

			out[out_stride * out_sample++] = sum;
			last_sample += int_advance;
			samp_frac_num += frac_advance;
			if (samp_frac_num >= den_rate) {
				samp_frac_num -= den_rate;
				last_sample++;
			}
		}

		st.last_sample[channel_index] = last_sample;
		st.samp_frac_num[channel_index] = samp_frac_num;
		return out_sample;
	}

}
